﻿using System.Linq;
using System.Web.Mvc;

namespace dPet.Web.Hospital.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.Services;
	using dPet.Web.Hospital.Fliters;
	using dPet.Web.Hospital.ViewModels;
	using Hangerd.Mvc;

	public class ServiceController : HangerdController
	{
		private readonly IServiceManagementService _serviceManagementService;

		public ServiceController(IServiceManagementService serviceManagementService)
		{
			_serviceManagementService = serviceManagementService;
		}

		#region Category

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceCategoryManagement)]
		public ActionResult CategoryList()
		{
			var categories = _serviceManagementService.GetServiceCategories(LoginStaffModel.Current.HospitalId);

			return View(categories);
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceCategoryManagement)]
		public ActionResult Category(string id)
		{
			var category = !string.IsNullOrWhiteSpace(id)
				? _serviceManagementService.GetServiceCategory(id)
				: new ServiceCategoryDto { HospitalId = LoginStaffModel.Current.HospitalId };

			if (category == null)
			{
				return RedirectToAction("CategoryList", "Service");
			}

			return View(category);
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceCategoryManagement)]
		public ActionResult SaveCategory(ServiceCategoryDto categoryDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(categoryDto.Id)
				? _serviceManagementService.UpdateServiceCategory(categoryDto.Id, categoryDto, loginStaff)
				: _serviceManagementService.AddServiceCategory(categoryDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Service/CategoryList"));
		}

		#endregion

		#region SKU Property

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement)]
		public ActionResult SKUPropertyList()
		{
			var properties = _serviceManagementService.GetServiceSKUProperties(LoginStaffModel.Current.HospitalId, true);

			return View(properties);
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement)]
		public ActionResult SKUProperty(string id)
		{
			var loginStaff = LoginStaffModel.Current;
			var categories = _serviceManagementService.GetServiceCategories(loginStaff.HospitalId);

			if (categories == null || categories.Count() == 0)
			{
				return base.AlertAndRedirect("请至少添加一个分类", string.Format("/Service/Category"));
			}

			var property = !string.IsNullOrWhiteSpace(id)
				? _serviceManagementService.GetServiceSKUProperty(id, true, false)
				: new ServiceSKUPropertyDto { HospitalId = loginStaff.HospitalId };

			if (property == null)
			{
				return RedirectToAction("SKUPropertyList", "Service");
			}

			return View(new ServiceSKUPropertyModel
			{
				SKUProperty = property,
				Categories = categories
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement)]
		public ActionResult SaveSKUProperty(ServiceSKUPropertyDto skuPropertyDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(skuPropertyDto.Id)
				? _serviceManagementService.UpdateServiceSKUProperty(skuPropertyDto.Id, skuPropertyDto, loginStaff)
				: _serviceManagementService.AddServiceSKUProperty(skuPropertyDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Service/SKUPropertyList"));
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement)]
		public ActionResult SKUPropertyValues(string id)
		{
			var property = _serviceManagementService.GetServiceSKUProperty(id, false, false);

			if (property == null)
			{
				return RedirectToAction("SKUPropertyList", "Service");
			}

			return View(property);
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult SaveSKUPropertyValue(string propertyId, ServiceSKUPropertyValueDto propertyValueDto)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.SaveServiceSKUPropertyValue(propertyId, propertyValueDto, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		#endregion

		#region Service Item

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement)]
		public ActionResult ServiceItemList()
		{
			var loginStaff = LoginStaffModel.Current;
			var items = _serviceManagementService.GetServiceItems(loginStaff.HospitalId, true, false);

			return View(items);
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement)]
		public ActionResult ServiceItem(string id)
		{
			var loginStaff = LoginStaffModel.Current;
			var categories = _serviceManagementService.GetServiceCategories(loginStaff.HospitalId);

			if (categories == null || categories.Count() == 0)
			{
				return base.AlertAndRedirect("请至少添加一个分类", string.Format("/Service/Category"));
			}

			var item = !string.IsNullOrWhiteSpace(id)
				? _serviceManagementService.GetServiceItem(id, true, false, false)
				: new ServiceItemDto { HospitalId = loginStaff.HospitalId };

			if (item == null)
			{
				return RedirectToAction("ServiceItemList", "Service");
			}

			return View(new ServiceItemModel
			{
				ServiceItem = item,
				Categories = categories
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement)]
		public ActionResult SaveServiceItem(ServiceItemDto serviceItemDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(serviceItemDto.Id)
				? _serviceManagementService.UpdateServiceItem(serviceItemDto.Id, serviceItemDto, loginStaff)
				: _serviceManagementService.AddServiceItem(serviceItemDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Service/ServiceItemList"));
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult AddSKUPropertyToServiceItem(string serviceItemId, string propertyId)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.AddSKUPropertyToServiceItem(serviceItemId, propertyId, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceSKUPropertyManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult RemoveSKUPropertyFromServiceItem(string serviceItemId, string propertyId)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.RemoveSKUPropertyFromServiceItem(serviceItemId, propertyId, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult RegenerateSKUForServiceItem(string serviceItemId)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.RegenerateSKUForServiceItem(serviceItemId, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult RemoveSKUFromServiceItem(string serviceItemSKUId)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.RemoveSKUFromServiceItem(serviceItemSKUId, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ServiceItemManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult UpdateServiceItemSKU(ServiceItemSKUDto skuDto)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _serviceManagementService.UpdateServiceItemSKU(skuDto.Id, skuDto, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		#endregion
	}
}
